﻿package com.thorqq.magictimer.timeLoop;

import java.util.Calendar;

import com.thorqq.magictimer.R;
import com.thorqq.magictimer.core.ChildView;
import com.thorqq.magictimer.core.ChildViewParent;
import com.thorqq.magictimer.core.TTimerDef;
import com.thorqq.magictimer.util.Util;
import com.thorqq.magictimer.widget.WheelView.NumericWheelAdapter;
import com.thorqq.magictimer.widget.WheelView.WheelView;
import com.thorqq.magictimer.widget.WheelView.OnWheelChangedListener;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ScrollView;


public class ChildViewTimeDef extends ChildView
{
    @SuppressWarnings("unused")
    private ChildViewParent mParent;
    private Context mContext;
    private LayoutInflater mInflater;

    private TTimerDef mTimerDef;
    private EditText mEtName;
    private EditText mEtMaxCount;
    private EditText mEtInterval;
    private CheckBox mCBInfinity;
    private Button   mBtnRemark;
    
    private WheelView mHour;
    private WheelView mMins;
    private Button mBtnNow;
     
    public ChildViewTimeDef(Context context, TTimerDef timerDef)
    {
        mContext = context;
        mTimerDef = timerDef;
    }

    @Override
    public void updateLayout()
    {        
        mHour.setCurrentItem(mTimerDef.getStartHour());
        mMins.setCurrentItem(mTimerDef.getStartMinute());

        mEtName.setText(mTimerDef.getName());
        mEtInterval.setText(Util.intToStr(mTimerDef.getInterval()));
        
        if(mTimerDef.getMaxCount() == TimerCalculator.INFINITY_COUNT)
        {
            mCBInfinity.setChecked(true);
            mEtMaxCount.setEnabled(false);
            mEtMaxCount.setText("1");
        }
        else if(mTimerDef.getMaxCount() >= 1)
        {
            mCBInfinity.setChecked(false);
            mEtMaxCount.setEnabled(true);
            mEtMaxCount.setText(Util.intToStr(mTimerDef.getMaxCount()));
        }
        else
        {
            mCBInfinity.setChecked(false);
            mEtMaxCount.setEnabled(true);
            mEtMaxCount.setText("1");
        }
        
        if(mTimerDef.getMaxCount() == 1)
        {
            mEtInterval.setEnabled(false);
        }
        else
        {
            mEtInterval.setEnabled(true);
        }

        
    }

    @Override
    protected void doInitLayout(ChildViewParent parent)
    {
        Util.log("init ChildViewTimeDef");
        mInflater = LayoutInflater.from(mContext);
        mView = mInflater.inflate(R.layout.timedef_item_child, null);
        mParent = parent;
        
        mEtName     = (EditText) mView.findViewById(R.id.editTextName);
        mEtMaxCount = (EditText) mView.findViewById(R.id.editTextMaxCount);
        mCBInfinity = (CheckBox) mView.findViewById(R.id.checkBoxInfinity);
        mEtInterval = (EditText) mView.findViewById(R.id.editTextInterval);   
        mBtnRemark  = (Button)   mView.findViewById(R.id.btnRemark);
        mBtnNow     = (Button)   mView.findViewById(R.id.btnNow);
        
        mHour = (WheelView) mView.findViewById(R.id.hour);
        mHour.setAdapter(new NumericWheelAdapter(0, 23));
        mHour.setLabel("时");
        mHour.setCyclic(true);

        mMins = (WheelView) mView.findViewById(R.id.mins);
        mMins.setAdapter(new NumericWheelAdapter(0, 59, "%02d"));
        mMins.setLabel("分");
        mMins.setCyclic(true);
    }
    
    

    @Override
    protected void doSetParentScrollView(ScrollView parentScrollView)
    {
        mHour.setParentScrollView(parentScrollView);
        mMins.setParentScrollView(parentScrollView);
    }

    protected void registerListener()
    {
        OnWheelChangedListener wheelListener = new OnWheelChangedListener() {
            public void onChanged(WheelView wheel, int oldValue, int newValue) {
                mTimerDef.setStartHour(mHour.getCurrentItem());
                mTimerDef.setStartMinute(mMins.getCurrentItem());
            }
        };

        mHour.addChangingListener(wheelListener);
        mMins.addChangingListener(wheelListener);
        
        mBtnNow.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View arg0)
            {
                Calendar now = Calendar.getInstance();
                int hour = now.get(Calendar.HOUR_OF_DAY);
                int mins = now.get(Calendar.MINUTE);
                
                mHour.setCurrentItem(hour, true);
                mMins.setCurrentItem(mins, true);
                
            }
        });
                
        mCBInfinity.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener()
        {
            @Override
            public void onCheckedChanged(CompoundButton buttonView,
                    boolean isChecked)
            {
                mEtMaxCount.setEnabled(!isChecked);
                
                if(isChecked)
                {
                    mTimerDef.setMaxCount(TimerCalculator.INFINITY_COUNT);
                }
                else
                {
                    mTimerDef.setMaxCount(Util.strToInt(mEtMaxCount.getText().toString()));
                }
                
                updateLayout();
            }
        });
        
        mEtName.addTextChangedListener(new TextWatcher()
        {

            @Override
            public void afterTextChanged(Editable arg0)
            {                
            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3)
            {                
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count)
            {
                mTimerDef.setName(s.toString());
            }
            
        });
        
        mBtnRemark.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View arg0)
            {
                final EditText editText = new EditText(mContext);
                editText.setText(mTimerDef.getRemark());
                
                new AlertDialog.Builder(mContext)
                    .setTitle("备注")
                    .setIcon(android.R.drawable.ic_dialog_info)
                    .setView(editText)
                    .setPositiveButton("确定", new DialogInterface.OnClickListener() 
                    {
                        public void onClick(DialogInterface dialog, int which) 
                        {
                            mTimerDef.setRemark(editText.getText().toString());
                        }
                    })
                    .setNegativeButton("取消", new DialogInterface.OnClickListener() 
                    {
                        public void onClick(DialogInterface dialog, int which) 
                        {
                            dialog.dismiss();
                        }
                    }).show();
            }
        });

        mEtMaxCount.addTextChangedListener(new TextWatcher()
        {

            @Override
            public void afterTextChanged(Editable arg0)
            {                
            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3)
            {                
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count)
            {
                if(mCBInfinity.isChecked())
                {
                    mTimerDef.setMaxCount(TimerCalculator.INFINITY_COUNT);
                }
                else
                {
                    mTimerDef.setMaxCount(Util.strToInt(s.toString()));
                } 

                if(Util.strToInt(s.toString()) == 1)
                {
                    mEtInterval.setEnabled(false);
                }
                else
                {
                    mEtInterval.setEnabled(true);
                }
            }
            
        });

        mEtInterval.addTextChangedListener(new TextWatcher()
        {

            @Override
            public void afterTextChanged(Editable arg0)
            {                
            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1, int arg2, int arg3)
            {                
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count)
            {
                mTimerDef.setInterval(Util.strToInt(s.toString()));
            }
            
        });

    }

    @Override
    protected void doSetTag(Object object)
    {
        if(object instanceof TTimerDef)
        {
            mTimerDef = (TTimerDef)object;
        }
    }

    @Override
    public void onClose()
    {
        
    }

}
